<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    
	public function index()
	{
        // Helpers
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        // Validation rules
        $this->form_validation->set_rules('email', 'Email address', 'required|valid_email');
		$this->form_validation->set_rules('password', 'Password', 'required');

        // Incoming variables from form
        $str_email = $this->input->post('email');
        $str_password = $this->input->post('password');

        // Check
        if ($this->form_validation->run() == FALSE)
		{
            // Load view
            $this->show_login_form();
		}
		else {
            if($str_email == "hello@johandorper.com" && $str_password == "test") {
                // Validate login with database
                $this->validate_user($str_email,$str_password);

                // Redirect to app
                redirect('/site/', 'refresh');
            } else {
                // Load view
                $data['custom_error'] = "Login incorrect, please try again.";
                $this->show_login_form($data);
            }

		}


	}

    private function show_login_form($data = array("custom_error" => "In order to gain access, you need to login."))
    {
        // Load view
        $this->parser->parse('tmpl/head');
        $this->parser->parse('login', $data);
        $this->parser->parse('tmpl/footer');
    }

    private function validate_user($email, $user) 
    {
        // Set session
        $this->session->set_userdata('login_success', true);
        $this->session->set_userdata('user_id', 1);
        $this->session->set_userdata('user_profile_name', "Johan Dorper");
    }
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */